# -*- coding:utf-8 -*-
import re
from demo.config import danyifuFile,duoyifuFile,chengduifuFile,spaceFlag,chineseCharacter,numberFile,characterFile,continueNumFlag,continueCharFg,addressFile,emailFile,urlFile,qqFile,phoneFile,telphoneFile,weichatFile,specialNameFile,standardNameflag,proviceNameFile,cityNameFile,countyNameFile,companySuffixFile828,companyMaxLength,companyMinLength
from corpusPickleHandler import readPickle

gdict={u'零':'0',u'壹':'1',u'贰':'2',u'叁':'3',u'肆':'4',u'伍':'5',u'陆':'6',u'柒':'7',u'捌':'8',u'玖':'9'}
ddict={u'一':'1',u'二':'2',u'三':'3',u'四':'4',u'五':'5',u'六':'6',u'七':'7',u'八':'8',u'九':'9'}

danyifu=readPickle(danyifuFile)
duoyifu=readPickle(duoyifuFile)
chengduifu=readPickle(chengduifuFile)
numberfu=readPickle(numberFile)
characterfu=readPickle(characterFile)
# 常用词
addres=readPickle(addressFile)
email=readPickle(emailFile)
url=readPickle(urlFile)
QQ=readPickle(qqFile)
phone=readPickle(phoneFile)
telphone=readPickle(telphoneFile)
weichat=readPickle(weichatFile)
specialName=readPickle(specialNameFile)
# 行政区划信息
provice=readPickle(proviceNameFile)
provice.append(u'集团')

city=readPickle(cityNameFile)
county=readPickle(countyNameFile)
# 公司名称后缀信息
companySuffix=readPickle(companySuffixFile828)
# qq常用词中容易引起混乱，进行临时替换
qqTempFlag=u'扣：|Ｑ：|Q：|Q:'
addTempFlag=u'Ａ：|A:'
emailTempFlag=u'Ｅ：|邮：|E:'
weichatTempFlag=u'Ｖ：|微：|V:|wechat号'
webTempFlag=u'ｈｔｔｐ：'
telTempFlag=u'Ｔ：|Ｆ：|电：|T:|F:'
phoneTempFlag=u'Ｍ：|M:|手机号码'

# 第二步识别
def step2Handler(text):
    qqtext=tempReplaceHandler(text,qqTempFlag,u"QQ")
    addtext=tempReplaceHandler(qqtext,addTempFlag,u"地址")
    emailtext=tempReplaceHandler(addtext,emailTempFlag,u"邮箱")
    weichattext=tempReplaceHandler(emailtext,weichatTempFlag,u"微信")
    webtext=tempReplaceHandler(weichattext,webTempFlag,u"网址")
    teltext=tempReplaceHandler(webtext,telTempFlag,u"座机")
    phonetext=tempReplaceHandler(teltext,phoneTempFlag,u"手机")

    text=chineseCharacterRepalce(phonetext)

    text=danYiFuHandle(text)

    text=duoYiFuHandle(text)

    text=text.replace("\t"," ")
    res=chengduifuHandle(text)
    return res

def chineseCharacterRepalce(text):
    return re.sub(chineseCharacter,u" ",text)

def danYiFuHandle(text):
    temp=danyifu['half']+danyifu['full']
    tmp=u'|'.join(set(temp))
    res=re.sub(tmp,u" ",text)
    return res

def duoYiFuHandle(text):

    temp=duoyifu['full']
    tmp=u'|'.join(temp)
    return re.sub(tmp,u" ",text)

def chengduifuHandle(text):
    temp=chengduifu['full']
    flag=[]

    for each in temp:
        item=each.split(' ')
        flag+=item

    flag=u'|'.join(flag)
    textList=text.split('\n')
    res=[]
    for line in textList:
        shibiejieguo=re.findall(flag,line)
        tmp=qianTaoHandler(shibiejieguo,temp,line)

        for each in tmp.split('\n'):
            chengduifuFlag=re.findall(flag,each)
            if isQiantao(chengduifuFlag,temp):
                res.append(each)
            else:
                #需要对非嵌套成对符进行处理，移动处理
                # try:
                    linshi=moveChengduifuContent(temp,chengduifuFlag,each)
                    res.append(linshi)
                # except:
                #     continue
    return '\n'.join(res)

def moveChengduifuContent(yuanshiFlag,flag,line):
    flagNum=len(flag)
    if flagNum==0:
        return line
    elif (flagNum==2 and isChengdui(flag,yuanshiFlag)):
        flag2=u'|'.join(flag)
        splitRes=re.split(flag2,line)
        res=splitRes[0].split(' ')
        res.insert(-1,u" "+splitRes[1])
        res.append(splitRes[-1])
        tmp=" ".join(res)
        return tmp.strip()
    elif flagNum >=4 and flagNum%2==0:
        return moveManyContent(yuanshiFlag,flag,line)
    else:
        return line

def qianTaoHandler(resList,yuanshiList,line):
    # 对嵌套进行分行处理
    positionList=[]
    position=0
    for item in resList:
        temp=line.find(item,position)
        positionList.append(temp)
        position=temp+1

    fenhangList=[]

    qianTaoPositionList=[]
    flagNum=len(resList)
    flag=0
    if flagNum%2==0:
        for i in xrange(0,flagNum,2):
            temp=resList[i]+u' '+resList[i+1]
            if temp not in yuanshiList:
                flag+=1
                if flag%2==1:
                    tmp=positionList[i]
                    qianTaoPositionList.append(tmp)
                else:
                    tmp=positionList[i+1]+1
                    qianTaoPositionList.append(tmp)
            else:
                continue
    else:
        return line

    sliceNum=len(qianTaoPositionList)
    if sliceNum==0:
        return line
    else:
        for j in xrange(0,sliceNum):
            if j ==0:
                fenhangList.append(line[0:qianTaoPositionList[j]])
            elif j == sliceNum-1:
                fenhangList.append(line[qianTaoPositionList[j]:])
            else:
                fenhangList.append(line[qianTaoPositionList[j-1]:qianTaoPositionList[j]])
        return '\n'.join(fenhangList)

def isChengdui(resList,yuanshiList):
    temp=u" ".join(resList)
    for item in yuanshiList:
        if item==temp:
            return True
    return False

def isQiantao(resList,yuanshiList):
    flagNum=len(resList)
    if flagNum!=0 and flagNum%2==0  :
        for i in xrange(0,flagNum,2):
            temp=resList[i]+u' '+resList[i+1]
            if temp not in yuanshiList:
                return True
    else:
        return False

def moveManyContent(yuanshiList,flagList,line):
    flagNum=len(flagList)
    for i in xrange(0,flagNum,2):
        temp=flagList[i]+u' '+flagList[i+1]
        if temp not in yuanshiList:
            return line
    #此处表示都是普通成对符
    flagPositon=[]
    position=0
    for item in flagList:
        temp=line.find(item,position)
        flagPositon.append(temp)
        position=temp+1

    #对成对符进行切片
    positionLength=len(flagPositon)


    tmp=range(0,positionLength,2)
    # print "++++++++++++++++++++++++++"
    # print flagPositon
    # print tmp
    # print "++++++++++++++++++++++++++"
    tmp.reverse()
    # print "++++++++++++++++++++++++++"
    # print tmp
    # print "++++++++++++++++++++++++++"
    if flagPositon[0]==0:
        pass
    else:
        houduan=None
        context=None
        for i in tmp:
            if i ==tmp[-1]:
                qianduanTemp=line[0:flagPositon[i]]
                if u' ' not in qianduanTemp:
                    context=context
                    houduan=qianduanTemp+houduan
                    return context+houduan
                else:
                    res=qianduanTemp.split(' ')
                    res.insert(-1,u" "+context)
                    res.append(houduan)
                    result=u" ".join(res)
                    return result.strip()
            elif i==tmp[0]:
                j=i-2
                houduanTemp=line[flagPositon[i+1]+1:]
                contextTemp=line[flagPositon[i]+1:flagPositon[i+1]]
                qianduanTemp=line[flagPositon[j+1]+1:flagPositon[i]]

                if u' ' not in qianduanTemp:
                    houduan=qianduanTemp+houduanTemp
                    context=line[flagPositon[j]+1:flagPositon[j+1]]+contextTemp
                else:
                    res=qianduanTemp.split(' ')
                    res.insert(-1,u" "+contextTemp)
                    res.append(houduanTemp)
                    houduan=u" ".join(res)
                    context=line[flagPositon[j]+1:flagPositon[j+1]]
            else:
                j=i-2
                qianduanTemp=line[flagPositon[j+1]+1:flagPositon[i]]
                contextTemp=line[flagPositon[j]+1:flagPositon[j+1]]
                if u' ' not in qianduanTemp:
                    context=contextTemp+context
                    houduan=qianduanTemp+houduan
                else:
                    res=qianduanTemp.split(' ')
                    res.insert(-1,u" "+context)
                    res.append(houduan)
                    houduan=u" ".join(res)
                    context=contextTemp


# 第三步识别
# 总体操作
def step3Handler(text):
    # 1-3-0
    temp=spaceStepOneHandler(text)
    # 1-3-1
    tmp=u""
    for each in temp.split(" "):
        tmp=numSpaceHandler(tmp,each)
    text=tmp.strip()
    #1-3-2
    text=replacingHandler(text)
    #1-3-3
    # -----------------------------------
    '''
    number=numberfu['half']
    character=characterfu['half']
    halfDuoyifu=duoyifu['half']
    #>4
    zuhe4=number+character+halfDuoyifu
    tmp4=''.join(zuhe4)
    flag4=r'(['+tmp4+']{5,})'
    text=continueHandler(text,flag4)
    #>3
    zuhe31=number+halfDuoyifu
    tmp31=''.join(zuhe31)
    flag31=r'(['+tmp31+']{4,})'
    text=continueHandler(text,flag31)
    zuhe32=character+halfDuoyifu
    tmp32=''.join(zuhe32)
    flag32=r'(['+tmp32+']{4,})'
    text=continueHandler(text,flag32)
    #>2
    text=continueHandler(text,continueNumFlag)
    text=continueHandler(text,continueCharFg)
    # ----------------------------------------
    '''
    # =======================================
    # 添加新代码
    number=numberfu['half']
    character=characterfu['half']
    halfDuoyifu=duoyifu['half']

    zuhe4=number+character+halfDuoyifu
    tmp4=''.join(zuhe4)
    flag4=r'(['+tmp4+']{5,})'
    #>3
    zuhe31=number+halfDuoyifu
    tmp31=''.join(zuhe31)
    flag31=r'(['+tmp31+']{4,})'
    zuhe32=character+halfDuoyifu
    tmp32=''.join(zuhe32)
    flag32=r'(['+tmp32+']{4,})'

    text=continueHandler4(text,flag4,flag31,flag32,continueNumFlag,continueCharFg)

    # =======================================
    #1-3-4
    result=spaceStepOneHandler(text)
    return result
# 1-3-4替换为1-3-0,同步进行
def spaceStepOneHandler(text):
    textList=re.split(spaceFlag,text)
    result=[]

    for item in textList:
        if len(item)==0:
            continue
        else:
            result.append(item)

    return u' '.join(result)
# 1-3-1
def numSpaceHandler(str1,str2):
    arabflag=('1','2','3','4','5','6','7','8','9','0')
    chineseflag=tuple(gdict.keys()+ddict.keys())

    temp11=str1.endswith(arabflag)
    temp12=str2.startswith(chineseflag)
    res1=temp11 & temp12

    temp21=str1.endswith(chineseflag)
    temp22=str2.startswith(arabflag)
    res2=temp21 & temp22

    res= res1 | res2
    if res:
        return str1+str2
    else:
        return str1+u" "+str2
# 1-3-2
def replacingHandler(string):
    res=""
    for item in string:
        item1=strQ2B(item)    #全角转半角
        if item1 in gdict:
            res+=gdict[item1]     #中文数字替换
        elif item1 in ddict:
            res+=ddict[item1]     #汉字数据替换
        else:
            res+=item1
    # print res
    res=res.lower()   #对英文大写进行处理
    return res
def strQ2B(uchar):
    # 全角-半角、大写-小写
    inside_code=ord(uchar)
    # print inside_code
    if inside_code == 12288 or inside_code == 160: #全角空格直接转换
        inside_code = 32
    elif (inside_code >= 65281 and inside_code <= 65374): #全角字符（除空格）根据关系转化
        inside_code -= 65248
    temp=unichr(inside_code)
    return temp
# 1-3-3 1、2、3

def continueHandler4(text,flag4,flag31,flag32,flag21,flag22):
    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag4,each)
            # if len(res)==0 or len(res)==1:
            if len(res)==0:
                zhongjian=continueHandler31(each,flag31,flag32,flag21,flag22)
                linshi.append(zhongjian)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler31(temp[0],flag31,flag32,flag21,flag22)
                    # -----------------------
                    if item.endswith("."):
                        linshi1=zhongjian+u" "+item
                    else:
                        linshi1=zhongjian+u" "+item+u" "
                    # -----------------------
                    # linshi1=zhongjian+u" "+item+u" "
                    tmp=temp[-1]
                    # if temp[0]==u'' and len(temp)==1:
                    if (temp[0]==u'' or len(temp)==1) or len(res)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)


def continueHandler31(text,flag31,flag32,flag21,flag22):
    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag31,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler32(temp[0],flag32,flag21,flag22)
                    if item.endswith("."):
                        linshi1=zhongjian+u" "+item
                    else:
                        linshi1=zhongjian+u" "+item+u" "
                    if (temp[0]==u'' or len(temp)==1) or len(res)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                    tmp=temp[-1]
                    # linshi1=zhongjian+u" "+item+u" "
                    # tmp=temp[-1]
                    # if temp[0]==u'':
                    #     guodu=guodu+linshi1+temp[-1]
                    # else:
                    #     guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)
def continueHandler32(text,flag32,flag21,flag22):

    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in line.split(' '):
            res=re.findall(flag32,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler2(temp[0],flag21)
                    zhongjian=continueHandler2(zhongjian,flag22)
                    if item.endswith("."):
                        linshi1=zhongjian+u" "+item
                    else:
                        linshi1=zhongjian+u" "+item+u" "
                    if (temp[0]==u'' or len(temp)==1) or len(res)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                    tmp=temp[-1]

                    # linshi1=zhognjian+u" "+item+u" "
                    # tmp=temp[-1]
                    # if temp[0]==u'':
                    #     guodu=guodu+linshi1+temp[-1]
                    # else:
                    #     guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)

def continueHandler2(text,flag):
    '''
    :param line:一行一行处理
    :param flag:
    :return:
    '''
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in line.split(' '):
            res=re.findall(flag,each)
            if len(res)==0:
                linshi.append(each)
            else:
                for item in res:
                    temp=re.split(item,each)
                    each=temp[0]+u" "+item+u" "+temp[-1]
                linshi.append(each)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)





'''
def continueHandler4(text,flag4,flag31,flag32,flag21,flag22):
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag4,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler31(temp[0],flag31,flag32,flag21,flag22)
                    linshi1=zhongjian+u" "+item+u" "
                    tmp=temp[-1]
                    if temp[0]==u'' or len(temp)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)


def continueHandler31(text,flag31,flag32,flag21,flag22):
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag31,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhongjian=continueHandler32(temp[0],flag32,flag21,flag22)
                    linshi1=zhongjian+u" "+item+u" "
                    tmp=temp[-1]
                    if temp[0]==u'' or len(temp)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)
def continueHandler32(text,flag32,flag21,flag22):
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag32,each)
            if len(res)==0:
                linshi.append(each)
            else:
                guodu=u""
                tmp=each
                for item in res:
                    temp=re.split(item,tmp)
                    zhognjian=continueHandler2(temp[0],flag21)
                    zhognjian=continueHandler2(zhognjian,flag22)
                    linshi1=zhognjian+u" "+item+u" "
                    tmp=temp[-1]
                    if temp[0]==u'' or len(temp)==1:
                        guodu=guodu+linshi1+temp[-1]
                    else:
                        guodu=guodu+linshi1
                linshi.append(guodu)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)

def continueHandler2(text,flag):
    result=[]
    for line in text.split('\n'):
        linshi=[]
        for each in re.split(' |	',line):
            res=re.findall(flag,each)
            if len(res)==0:
                linshi.append(each)
            else:
                for item in res:
                    temp=re.split(item,each)
                    each=temp[0]+u" "+item+u" "+temp[-1]
                linshi.append(each)
        eachLine=u' '.join(linshi)
        result.append(eachLine.strip())
    return '\n'.join(result)

'''
# def continueHandler(text,flag):
#     '''
#     :param line:一行一行处理
#     :param flag:
#     :return:
#     '''
#     result=[]
#     for line in text.split('\n'):
#         linshi=[]
#         for each in line.split(' '):
#             res=re.findall(flag,each)
#             if len(res)==0:
#                 linshi.append(each)
#             else:
#                 for item in res:
#                     temp=re.split(item,each)
#                     each=temp[0]+u" "+item+u" "+temp[-1]
#                 linshi.append(each)
#         eachLine=u' '.join(linshi)
#         result.append(eachLine.strip())
#     return '\n'.join(result)
# 第四步处理
def step4Handler(text):
    #address
    addresFlag=u"|".join(addres)
    text=commonWordReplace(text,addresFlag,u"地址")
    #email
    emailFlag=u"|".join(email)
    text=commonWordReplace(text,emailFlag,u"邮箱")

    #url
    urlFlag=u"|".join(url)
    text=commonWordReplace(text,urlFlag,u"网址")

    #qq
    qqFlag=u"|".join(QQ)
    print "-------------"
    print "qq"
    print "-------------"
    text=commonWordReplace(text,qqFlag,u"QQ")

    #phone
    phoneFlag=u"|".join(phone)
    text=commonWordReplace(text,phoneFlag,u"手机")

    #telphone
    telphoneFlag=u"|".join(telphone)
    text=commonWordReplace(text,telphoneFlag,u"座机")

    #weichat
    weichatFlag=u"|".join(weichat)
    text=commonWordReplace(text,weichatFlag,u"微信")

    #specialName
    specialNameFlag=u"|".join(specialName)
    text=commonWordReplace(text,specialNameFlag,u"特例名")
    #1-4-2
    text=standardNameSpaceHandler(text)

    #1-4-3
    res=spaceStepOneHandler(text)

    return res


# 1-4-1常用词替换:地址、邮箱、网址、QQ、手机、座机、微信、特例名、企业名称
def commonWordReplace(text,targetFlag,replaceObj):
    temp=re.sub(targetFlag,replaceObj,text)
    if replaceObj==u"QQ":
        return commonWordQQAux(temp)
    else:
        return temp

def commonWordQQAux(text):
    text=re.sub("@QQ","@qq",text)
    return re.sub("@vip.QQ","@vip.qq",text)

#1-4-2处理标准输出名称，在同一行中有连续标准名称时进行空格添加操作
def standardNameSpaceHandler(text):
    result=[]
    for line in text.split("\n"):
        linshi=[]
        for each in line.split(" "):
            temp=standardNameSpaceHandlerAux(each,standardNameflag)
            linshi.append(temp)
        tmp=u' '.join(linshi)
        result.append(tmp)
    return '\n'.join(result)
def standardNameSpaceHandlerAux(text,flag):
    temp=re.findall(flag,text)
    tempLength=len(temp)
    if tempLength<=1:
        return text
    else:
        result=u""
        i=1
        for item in temp:
            if i==1:
                tmp=re.split(item,text,1)
                res=tmp[0]+item
                result+=res
                text=tmp[-1]
                i+=1
            else:
                tmp=re.split(item,text,1)
                res=tmp[0]+u" "+item
                result+=res
                text=tmp[-1]
                if i==tempLength:
                    result+=text
                i+=1
        return result
# 1-4-3 多空格处理 使用1-3-4

# 第五步处理
def step5Handler(text):
    #1-5-1
    text=replaceSpaceHandler(text)
    #1-5-2
    text=blankLineHandler(text)
    return text
#1-5-1
def replaceSpaceHandler(text):
    return re.sub(" |\\t","\n",text)
#1-5-2
def blankLineHandler(text):
    temp=re.split("\n",text)
    res=[]
    for line in temp:
        line=line.strip()
        if len(line)==0:
            continue
        else:
            res.append(line)
    return '\n'.join(res)
# 企业名称
# 2-2-1
def stepCompanyNameHandler(line):
    line=line.replace('2',u"二").replace('3',u"三").replace('4',u"四").replace('5',u"五").replace('6',u"六").replace('7',u"七").replace('8',u"八").replace('9',u"九")
    prefix=isIncludeProviceOrCityOrConty(provice,city,county,line)
    if prefix == False:
        return False
    prefixLength=prefix[-1]

    suffix=isIncludeCompanySuffix(companySuffix,line)
    if suffix==False:
        return False
    suffixLength=suffix[-1]

    res=isAccordCompanyLength(line,prefixLength,suffixLength)
    return res
# 1
def isIncludeProviceOrCityOrConty(provice,city,county,line):
    targetProvice=u'|'.join(provice)
    targetCity=u'|'.join(city)
    targetCounty=u'|'.join(county)
    proviceRes=isIncludeTarget(targetProvice,line,'start')
    if proviceRes!=False:
        return proviceRes
    cityRes=isIncludeTarget(targetCity,line,'start')
    if cityRes!=False:
        return cityRes
    countyRes=isIncludeTarget(targetCounty,line,'start')
    if countyRes!=False:
        return countyRes
    else:
        return False
# 2
def isIncludeCompanySuffix(companySuffix,line):
    targetSuffix=u'|'.join(companySuffix)
    return isIncludeTarget(targetSuffix,line,'end')
# 3
def isAccordCompanyLength(line,prefixLength,suffixLength):
    length=len(line.strip())
    target=length-prefixLength-suffixLength
    if target <= companyMaxLength and target >= companyMinLength:
        return True
    else:
        return False

# 是否包含省、市、县区、公司后缀目标
def isIncludeTarget(target,line,direction):
    resList=re.findall(target,line)
    if len(resList)==0:
        return False
    else:
        if direction=='start':
            for item in resList:
                if line.startswith(item):
                    return True,len(item)
                else:
                    continue
            return False
        else:
            for item in resList:
                if line.endswith(item):
                    return True,len(item)
                else:
                    continue
            return False

# 临时添加，在开头将qq模块中容易出问题的几个符号替换成标准名称
def tempReplaceHandler(text,flag,target):
    return re.sub(flag,target,text)


